var path = require('path')
var webpack = require('webpack')
const ExtractTextPlugin = require('extract-text-webpack-plugin')
const TerserPlugin = require('terser-webpack-plugin-legacy')
console.log(path.resolve(__dirname,'./node_modules/mapbox-gl'))
module.exports = {
  entry: process.env.NODE_ENV=='development'?'./src/main.js':'./src/index.js',
  output: {
    path: path.resolve(__dirname, './dist'),
    publicPath: process.env.NODE_ENV=='development'?'/dist/':'../',
    filename: 'gykj-map-core.js',
    library: 'gykj-map-core',
    libraryTarget: 'umd', // libraryTarget会生成不同umd的代码,可以只是commonjs标准的，也可以是指amd标准的，也可以只是通过script标签引入的
    umdNamedDefine: true // // 会对 UMD 的构建过程中的 AMD 模块进行命名。否则就使用匿名的 define
  },
  module: {
    rules: [
      {
        test: /\.css$/,
        use: process.env.NODE_ENV=='development'? [
          'vue-style-loader',
          'css-loader'
        ]:ExtractTextPlugin.extract({
          fallback:'vue-style-loader',
          use:'css-loader'
        })
      },
      {
        test: /\.scss$/,
        use: [
          'vue-style-loader',
          'css-loader',
          'sass-loader'
        ],
      },
      {
        test: /\.less$/,
        use: process.env.NODE_ENV=='development'?[
          'vue-style-loader',
          'css-loader',
          'less-loader'
        ]:ExtractTextPlugin.extract({
          fallback:'vue-style-loader',
          use:['css-loader',
            'less-loader']
        }),
      },
      {
        test: /\.sass$/,
        use: [
          'vue-style-loader',
          'css-loader',
          'sass-loader?indentedSyntax'
        ],
      },
      {
        test: /\.vue$/,
        loader: 'vue-loader',
        options: {
          loaders: {
            // Since sass-loader (weirdly) has SCSS as its default parse mode, we map
            // the "scss" and "sass" values for the lang attribute to the right configs here.
            // other preprocessors should work out of the box, no loader config like this necessary.
            'scss': [
              'vue-style-loader',
              'css-loader',
              'sass-loader'
            ],
            'sass': [
              'vue-style-loader',
              'css-loader',
              'sass-loader?indentedSyntax'
            ]
          }
          // other vue-loader options go here
        }
      },
      {
        test: /\.js$/,
        loader: 'babel-loader',
        include: [
          path.resolve(__dirname,'./src'),
          // path.resolve(__dirname,'./node_modules/mapbox-gl')
        ],
      },
      {
        test: /\.(png|jpg|gif)$/,
        loader: 'file-loader',
        options: {
          name: '[name].[ext]?[hash]'
        }
      },
      { test: /\.(woff|ttf|eot|svg)/, loader: 'file-loader?name=font/[name].[ext]' }
    ]
  },
  resolve: {
    alias: {
      'vue$': 'vue/dist/vue.esm.js'
    },
    extensions: ['*', '.js', '.vue', '.json']
  },
  devServer: {
    historyApiFallback: true,
    noInfo: true,
    overlay: true
  },
  performance: {
    hints: false
  },
  devtool: '#eval-source-map',
  plugins: [
    new webpack.ProvidePlugin({
      jQuery: "jquery",
      $: "jquery"
    })
  ]
}

if (process.env.NODE_ENV === 'production') {
  module.exports.devtool = '#source-map'
  // http://vue-loader.vuejs.org/en/workflow/production.html
  module.exports.plugins = (module.exports.plugins || []).concat([
    new webpack.DefinePlugin({
      'process.env': {
        NODE_ENV: '"production"'
      }
    }),
    new ExtractTextPlugin('css/gykj-map-core.css'),//括号内为打包成css文件的名字位置
    new TerserPlugin({
      sourceMap: true,
      terserOptions: {
        compress: {
          warnings: false,
          comparisons: false,
          drop_console: true,
          pure_funcs: ["console.log"]
        },
      },
    }),
    new webpack.LoaderOptionsPlugin({
      minimize: true
    })
  ])
}
